﻿Imports System.Xml.Serialization

Namespace Commands

    Public Class GetShareItems
        Inherits Framework.Net.CommandBase

        Private Const mRequestCode As String = "GetShareItems"
        Private Const mReplyCode As String = "GetShareItemsReply"

        Public Sub New()
            MyBase.New(mRequestCode, mReplyCode)
        End Sub

#Region " Entities "

        Public Class Request
            <XmlAttribute()> Public ShareId As Integer
            <XmlText()> Public DirectoryCode As String
        End Class
        Public Class Reply
            Public Columns As New Generic.List(Of Column)
            Public Groups As New Generic.List(Of Group)
        End Class
        Public Class Group
            <XmlAttribute()> Public Name As String
            <XmlAttribute()> Public Hidden As Boolean
            Public Directories As New Generic.List(Of Directory)
            Public Items As New Generic.List(Of ItemInfo)
            Public Sub New()
            End Sub
            Public Sub New(ByVal name As String)
                Me.Name = name
            End Sub
            Public Sub New(ByVal name As String, ByVal hidden As Boolean)
                Me.Name = name : Me.Hidden = hidden
            End Sub
        End Class
        Public Class Column
            <XmlText()> Public Caption As String
            <XmlAttribute()> Public Alignment As HorizontalAlignment
            <XmlAttribute()> Public DefaultWidth As Integer
            Public Sub New()
            End Sub
            Public Sub New(ByVal caption As String, ByVal defaultWidth As Integer, ByVal alignment As HorizontalAlignment)
                Me.Caption = caption : Me.Alignment = alignment : Me.DefaultWidth = defaultWidth
            End Sub
            Public Enum HorizontalAlignment
                Left
                Center
                Right
            End Enum
        End Class
        Public Class ItemInfo
            <XmlAttribute()> Public Id As Integer
            <XmlAttribute()> Public Type As Entities.ItemType
            <XmlAttribute()> Public UniqueId As String
            Public Name As String
            Public Cells As New Generic.List(Of CellBase)
            Public ExternalIds As Generic.List(Of String)
        End Class
        Public Class CellDate
            Inherits CellBase
            <XmlAttribute()> Public Value As Date
            Public Sub New()
            End Sub
            Public Sub New(ByVal value As Date)
                Me.Value = value
            End Sub
            Public Overrides Function ToString() As String
                Return Value.ToString("g")
            End Function
        End Class
        Public Class CellString
            Inherits CellBase
            Public Sub New()
            End Sub
            Public Sub New(ByVal value As String)
                Me.Value = value
            End Sub
            <XmlAttribute()> Public Value As String
            Public Overrides Function ToString() As String
                Return Value
            End Function
        End Class
        Public Class CellFileSize
            Inherits CellBase
            Public Sub New()
            End Sub
            Public Sub New(ByVal value As Long)
                Me.Value = value
            End Sub
            <XmlAttribute()> Public Value As Long
            Public Overrides Function ToString() As String
                Return Framework.Misc.ToReadableFileSize(Value)
            End Function
        End Class
        <XmlInclude(GetType(CellFileSize)), XmlInclude(GetType(CellDate)), XmlInclude(GetType(CellString))> _
       Public MustInherit Class CellBase
            Public MustOverride Overrides Function ToString() As String
        End Class
        Public Class Directory
            <XmlAttribute()> Public Code As String
            <XmlAttribute()> Public IsSeparator As Boolean
            <XmlText()> Public Name As String
            <XmlAttribute()> Public Description As String
            <XmlAttribute()> Public TotalItems As Integer
            <XmlAttribute()> Public TotalSize As Long
            Public Sub New()
            End Sub
            Public Sub New(ByVal code As String, ByVal name As String, ByVal description As String)
                Me.Code = code : Me.Name = name : Me.Description = description
            End Sub
            Public Sub New(ByVal code As String, ByVal name As String)
                Me.Code = code : Me.Name = name
            End Sub
            Public Sub New(ByVal isSeparator As Boolean)
                Me.IsSeparator = isSeparator
            End Sub
        End Class
#End Region

        Public Delegate Sub ReplyDelegate(ByVal inReplyToMessageId As Integer, ByVal node As Framework.Net.Node, ByVal reply As Reply)
        Public Shared Function SendRequest(ByVal node As Framework.Net.Node, ByVal share As Entities.Infos.ShareInfo, ByVal directoryCode As String, ByVal callback As ReplyDelegate, ByVal invokeCallbackOnMainGuiThread As Boolean) As Integer
            Dim clsRequest As New Request
            clsRequest.ShareId = share.Id
            clsRequest.DirectoryCode = directoryCode

            Dim iMessageId As Integer = Framework.Net.Manager.GetNextMessageId()
            RegisterCallback(iMessageId, callback, invokeCallbackOnMainGuiThread)
            node.SendCommandMessageAsync(iMessageId, mRequestCode, Framework.Serialization.ToXmlBytes(clsRequest))
            Return iMessageId
        End Function

        Public Overrides Sub HandleReply(ByVal envelope As Framework.Net.MessageEnvelope)
            Dim clsReply As Reply = Framework.Serialization.FromXmlBytes(Of Reply)(envelope.Message.Message)
            InvokeCallback(envelope.Message.InReplyToId, envelope.Node, clsReply)
        End Sub

        Public Overrides Sub HandleRequest(ByVal envelope As Framework.Net.MessageEnvelope)
            Dim clsRequest As Request = Framework.Serialization.FromXmlBytes(Of Request)(envelope.Message.Message)

            Dim clsReply As Reply = Nothing

            Dim clsShare As Entities.Share = EntityHandlers.Shares.GetById(clsRequest.ShareId)
            If envelope.Node.BuddyKeyMaster.IsGranted(clsShare) Then
                If clsShare IsNot Nothing Then
                    Select Case clsShare.ItemTypes
                        Case Entities.ItemType.Movies
                            clsReply = GetMovies.GetReply(clsShare, clsRequest.DirectoryCode)
                        Case Entities.ItemType.Files
                            clsReply = GetFiles.GetReply(clsShare, clsRequest.DirectoryCode)
                        Case Else
                            clsReply = New Reply
                    End Select
                End If
            End If
            If clsReply Is Nothing Then clsReply = New Reply

            envelope.Node.SendReplyCommandMessageAsync(envelope, mReplyCode, Framework.Serialization.ToXmlBytes(clsReply))
        End Sub
    End Class


End Namespace
